package com.haibara.codesubmiteval.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haibara.codesubmiteval.annotation.AuthCheck;
import com.haibara.codesubmiteval.common.BaseResponse;
import com.haibara.codesubmiteval.common.DeleteRequest;
import com.haibara.codesubmiteval.common.ErrorCode;
import com.haibara.codesubmiteval.constant.AuthConstant;
import com.haibara.codesubmiteval.exception.BusinessException;
import com.haibara.codesubmiteval.mapper.QuestionMapper;
import com.haibara.codesubmiteval.model.dto.question.QuestionAddRequest;
import com.haibara.codesubmiteval.model.dto.question.QuestionEditRequest;
import com.haibara.codesubmiteval.model.dto.question.QuestionQueryRequest;
import com.haibara.codesubmiteval.model.entity.Question;
import com.haibara.codesubmiteval.model.vo.QuestionVO;
import com.haibara.codesubmiteval.service.QuestionService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目接口
 *
 * @Author: mengyu
 * @Date: 2025/2/3
 */
@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {
    @Resource
    private QuestionService questionService;
    @Resource
    private QuestionMapper questionMapper;

    /**
     * 新增题目
     *
     * @param questionAddRequest 题目新增请求
     * @return 执行信息
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Long> addQuestion(@RequestBody @Valid QuestionAddRequest questionAddRequest) {
        return BaseResponse.success(questionService.addQuestion(questionAddRequest));
    }

    /**
     * 删除题目
     *
     * @param deleteRequest 题目删除请求
     * @return 执行信息
     */
    @DeleteMapping
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> deleteQuestion(@RequestBody @Valid DeleteRequest deleteRequest) {
        questionService.deleteQuestion(deleteRequest.getId());
        return BaseResponse.success(true);
    }

    /**
     * 更新题目
     *
     * @param questionEditRequest 题目修改请求
     * @return 执行信息
     */
    @PutMapping
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> updateQuestion(@RequestBody @Valid QuestionEditRequest questionEditRequest) {
        questionService.updateQuestion(questionEditRequest);
        return BaseResponse.success(true);
    }

    /**
     * 获取题目详细信息
     *
     * @param id 题目id
     * @return 题目信息
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Question> getQuestion(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "题目id不能为空");
        }
        Question question = questionService.getById(id);
        if (question == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "题目不存在");
        }
        return BaseResponse.success(question);
    }

    @GetMapping("/top3")
    public BaseResponse<List<QuestionVO>> getTop3RatioQuestionVO(Boolean isHardest) {
        List<Question> questions = questionMapper.selectTop3Questions(isHardest);
        List<QuestionVO> result = questions.stream().map(QuestionVO::getQuestionVOByQuestion).toList();
        return BaseResponse.success(result);
    }

    /**
     * 获取题目脱敏信息
     *
     * @param id 题目id
     * @return 题目脱敏信息
     */
    @GetMapping("/get/vo")
    @AuthCheck(mustRole = AuthConstant.USER)
    public BaseResponse<QuestionVO> getQuestionVO(Long id) {
        QuestionVO result = questionService.getQuestionVO(id);
        return BaseResponse.success(result);
    }

    /**
     * 分页查询题目详细信息
     *
     * @param questionQueryRequest 题目分页查询请求
     * @return 题目分页信息
     */
    @PostMapping("/page")
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Page<Question>> pageQueryQuestion(@RequestBody QuestionQueryRequest questionQueryRequest) {
        Page<Question> questionPage = questionService.pageQueryQuestion(questionQueryRequest);
        return BaseResponse.success(questionPage);
    }

    /**
     * 分页查询题目详细信息
     *
     * @param questionQueryRequest 题目分页查询请求
     * @return 题目脱敏分页信息
     */
    @PostMapping("/page/vo")
    public BaseResponse<Page<QuestionVO>> pageQueryQuestionVO(@RequestBody QuestionQueryRequest questionQueryRequest) {
        Page<QuestionVO> questionVOPage = questionService.pageQueryQuestionVO(questionQueryRequest);
        return BaseResponse.success(questionVOPage);
    }

    /**
     * 获取下一道题目id
     *
     * @param id 当前题目id
     * @return 下一道题目id
     */
    @GetMapping("/next")
    @AuthCheck(mustRole = AuthConstant.USER)
    public BaseResponse<Long> getNextQuestionId(Long id) {
        Long nextQuestionId = questionMapper.getNextQuestionId(id);
        if (nextQuestionId == null) {
            // 没有下一道题
            return BaseResponse.error(ErrorCode.NOT_FOUND_ERROR.getCode(), "这是最后一道题了!");
        }
        return BaseResponse.success(nextQuestionId);
    }
}
